﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using Rizmar.RizmarDataSetTableAdapters;

namespace Rizmar
{
    public partial class TaxasForm : Form
    {
        public TaxasForm()
        {
            InitializeComponent();
        }

        private void buttonCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            TaxaTableAdapter tta = new TaxaTableAdapter();
            if (tta.Atualiza(Convert.ToDecimal(pisTextBox.Text.Replace('.', ',')), Convert.ToDecimal(cofinsTextBox.Text.Replace('.', ',')), Convert.ToDecimal(ioftextBox.Text.Replace('.', ',')), Convert.ToDecimal(cpmfTextBox.Text.Replace('.', ',')), Convert.ToDecimal(issTextBox.Text.Replace('.', ',')), Convert.ToDecimal(adValoreTextBox.Text.Replace('.', ','))) > 0)
            {
                MessageBox.Show("Taxas atualizadas com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }

        private void TaxasForm_Load(object sender, EventArgs e)
        {
            TaxaTableAdapter tta = new TaxaTableAdapter();
            RizmarDataSet.TaxaDataTable cdt = new RizmarDataSet.TaxaDataTable();
            cdt = tta.GetData();
            decimal bPis = 0, bCofins = 0, bIof = 0, bIss = 0, bCpmf = 0, bAdValore = 0;
            if (cdt.Rows.Count > 0)
            {
                string STpis = Convert.ToString(cdt.Rows[0]["pis"]).Replace('.', ',');
                string STcofins = Convert.ToString(cdt.Rows[0]["cofins"]).Replace('.', ',');
                string STiof = Convert.ToString(cdt.Rows[0]["iof"]).Replace('.', ',');
                string STiss = Convert.ToString(cdt.Rows[0]["iss"]).Replace('.', ',');
                string STcpmf = Convert.ToString(cdt.Rows[0]["cpmf"]).Replace('.', ',');
                string STadValore = Convert.ToString(cdt.Rows[0]["adValore"]).Replace('.', ',');
                bPis = Convert.ToDecimal(STpis);
                bCofins = Convert.ToDecimal(STcofins);
                bIof = Convert.ToDecimal(STiof);
                bIss = Convert.ToDecimal(STiss);
                bCpmf = Convert.ToDecimal(STcpmf);
                bAdValore = Convert.ToDecimal(STadValore);
            }
            pisTextBox.Text = Convert.ToString(bPis);
            cofinsTextBox.Text = Convert.ToString(bCofins);
            ioftextBox.Text = Convert.ToString(bIof);
            issTextBox.Text = Convert.ToString(bIss);
            cpmfTextBox.Text = Convert.ToString(bCpmf);
            adValoreTextBox.Text = Convert.ToString(bAdValore);
         }

        private void pisTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != ',')
                e.Handled = true;
            if (e.KeyChar == '.' && (pisTextBox.Text.Contains(".") || pisTextBox.Text.Contains(",")))
                e.Handled = true;
            if (e.KeyChar == ',' && (pisTextBox.Text.Contains(",") || pisTextBox.Text.Contains(".")))
                e.Handled = true;
            if (pisTextBox.Text.Contains(".") && (pisTextBox.Text.Length - pisTextBox.Text.IndexOf('.') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            if (pisTextBox.Text.Contains(",") && (pisTextBox.Text.Length - pisTextBox.Text.IndexOf(',') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void cofinsTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != ',')
                e.Handled = true;
            if (e.KeyChar == '.' && (cofinsTextBox.Text.Contains(".") || cofinsTextBox.Text.Contains(",")))
                e.Handled = true;
            if (e.KeyChar == ',' && (cofinsTextBox.Text.Contains(",") || cofinsTextBox.Text.Contains(".")))
                e.Handled = true;
            if (cofinsTextBox.Text.Contains(".") && (cofinsTextBox.Text.Length - cofinsTextBox.Text.IndexOf('.') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            if (cofinsTextBox.Text.Contains(",") && (cofinsTextBox.Text.Length - cofinsTextBox.Text.IndexOf(',') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void ioftextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != ',')
                e.Handled = true;
            if (e.KeyChar == '.' && (ioftextBox.Text.Contains(".") || ioftextBox.Text.Contains(",")))
                e.Handled = true;
            if (e.KeyChar == ',' && (ioftextBox.Text.Contains(",") || ioftextBox.Text.Contains(".")))
                e.Handled = true;
            if (ioftextBox.Text.Contains(".") && (ioftextBox.Text.Length - ioftextBox.Text.IndexOf('.') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            if (ioftextBox.Text.Contains(",") && (ioftextBox.Text.Length - ioftextBox.Text.IndexOf(',') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void issTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != ',')
                e.Handled = true;
            if (e.KeyChar == '.' && (issTextBox.Text.Contains(".") || issTextBox.Text.Contains(",")))
                e.Handled = true;
            if (e.KeyChar == ',' && (issTextBox.Text.Contains(",") || issTextBox.Text.Contains(".")))
                e.Handled = true;
            if (issTextBox.Text.Contains(".") && (issTextBox.Text.Length - issTextBox.Text.IndexOf('.') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            if (issTextBox.Text.Contains(",") && (issTextBox.Text.Length - issTextBox.Text.IndexOf(',') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void cpmfTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != ',')
                e.Handled = true;
            if (e.KeyChar == '.' && (cpmfTextBox.Text.Contains(".") || cpmfTextBox.Text.Contains(",")))
                e.Handled = true;
            if (e.KeyChar == ',' && (cpmfTextBox.Text.Contains(",") || cpmfTextBox.Text.Contains(".")))
                e.Handled = true;
            if (cpmfTextBox.Text.Contains(".") && (cpmfTextBox.Text.Length - cpmfTextBox.Text.IndexOf('.') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            if (cpmfTextBox.Text.Contains(",") && (cpmfTextBox.Text.Length - cpmfTextBox.Text.IndexOf(',') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }


        }
    
}
